home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 February / EnigmA AMIGA RUN 15 (1997)(G.R. Edizioni)(IT)[!][issue 1997-02][PLANET CD V].iso / enigma / earcd / utility / utilcli / rep.lha / rep.c < prev    next >
C/C++ Source or Header  |  1996-11-28  |  3KB  |  144 lines

  1.  
  2. /******** rep.c by Dimitri C. Keletsekis - 28 November 1996 ************
  3.  
  4. This code compiles into 820 bytes of executable re-entrant CLI command
  5. for replacing strings in files. It can be compiled with SAS C 6.50+. 
  6. The SCOPTIONS file is included. Just do <sc rep link> to produce it.
  7.  
  8. It's may not be the best, it may not be the fastest, but it works..
  9.  
  10. It uses the NOSTARTUP compiler option which means that it compiles
  11. without *any* start-up code from the compiler. It's one function with
  12. no global variables.
  13.  
  14. *************************************************************************/
  15.  
  16. #include <exec/exec.h>
  17. #include <exec/types.h>
  18. #include <exec/execbase.h>
  19. #include <exec/memory.h>
  20. #include <dos/dosextens.h>
  21. #include <dos/rdargs.h>
  22. #include <dos/dostags.h>
  23.  
  24. #include <string.h>
  25. #include <stdio.h>
  26. #include <ctype.h>
  27. #include <dos.h>
  28.  
  29. #include <proto/dos.h>
  30. #include <proto/exec.h>
  31.  
  32.  
  33. int rep(void)
  34. {
  35. struct DosLibrary  *DOSBase=NULL;
  36. struct RDArgs *rdargs = NULL;
  37. unsigned char *file, *str, *repstr, dest[256];
  38. long rc;
  39. long args[4];
  40. struct FileInfoBlock *fib=NULL;
  41. unsigned char *buff=NULL;
  42. BPTR fp;
  43. LONG buffsize, sz;
  44. int err = 0;
  45. register unsigned char *p, *d, *t;
  46. unsigned char *endfile;
  47. BOOL flag = 0;
  48.  
  49. rc = 10;   /* return code */
  50.  
  51. if (!(DOSBase = (struct DosLibrary *)OpenLibrary("dos.library", 36L)))
  52. { Write (Output(), "Need newer OS\n", 14);
  53.   goto endprog;
  54. }
  55.  
  56. memset((char *)args, 0, sizeof(args));
  57. rdargs = ReadArgs("FILE/A,STRING/A,NEWSTRING/A,NEWFILE", args, NULL);
  58. if (!rdargs)
  59. { Write (Output(), "Wrong arguments\n", 16);
  60.   goto endprog;
  61. }
  62.  
  63. file   = (char *)args[0];
  64. str    = (char *)args[1];
  65. repstr = (char *)args[2];
  66. if (args[3])
  67.     strcpy (dest, (char *)args[3]);
  68. else
  69.     strcpy (dest, (char *)args[0]);
  70.  
  71. /*------------------------------ read file ------------------------*/
  72.  
  73. if (!(fp = Open (file, MODE_OLDFILE))) 
  74. {  Write (Output(), "Could not open file\n", 20);
  75.    goto endprog;
  76. }
  77.  
  78. if (fib = (struct FileInfoBlock *)AllocVec(sizeof(struct FileInfoBlock), MEMF_CLEAR))
  79. {   if (ExamineFH(fp, fib))
  80.     {   buffsize = fib->fib_Size;
  81.         if (buff = (unsigned char *)AllocVec(buffsize + 32, MEMF_CLEAR))
  82.         {   sz = Read (fp, buff, buffsize);
  83.         if (sz != buffsize) ++err;
  84.             endfile = &buff[sz];
  85.         }
  86.     }
  87.     FreeVec (fib);
  88. }
  89. Close (fp);
  90.  
  91. if (err)
  92. {  Write (Output(), "Error reading file\n", 19);
  93.    goto endprog;
  94. }
  95. else if (!buff)
  96. {  Write (Output(), "No memory!\n", 11);
  97.    goto endprog;
  98. }
  99.  
  100. /*---------------------------- do replace ---------------------*/
  101.  
  102. if (!(fp = Open (dest, MODE_NEWFILE)))
  103. {  Write (Output(), "Error writing file\n", 19);
  104.    goto endprog;
  105. }
  106. p = buff;
  107.  
  108. while (p < endfile)
  109. {
  110.    if (*p == *str)
  111.    {   t = p;
  112.        d = str;
  113.        while ((*d) && (*t) && (*d == *t)) { ++t; ++d; }
  114.        if (*d == 0) 
  115.        {   --t;
  116.            p = t;
  117.            if (EOF == (int)(FPuts (fp, repstr))) goto endflag;
  118.        }
  119.        else
  120.            if (EOF == (int)(FPutC (fp, *p))) goto endflag;
  121.     }
  122.     else
  123.        if (EOF == (int)(FPutC (fp, *p))) goto endflag;
  124.  
  125.     ++p;
  126. }
  127.  
  128. rc = 0;
  129. goto endnoflag;  /* i.e. skip error report */
  130.  
  131. endflag:
  132. Write (Output(), "Error writing file\n", 19);
  133.  
  134. endnoflag:
  135. Close (fp);
  136.  
  137. endprog :
  138. if (rdargs)  FreeArgs (rdargs);
  139. if (buff)    FreeVec (buff);
  140. if (DOSBase) CloseLibrary((struct Library *)DOSBase);
  141. return (rc);
  142. }
  143.  
  144.